package com.lf.web.service.system.webnode;

import com.lf.data.file.Limit;
import com.lf.web.service.KeyUpdate;
import com.lf.web.service.ResponseCode;
import com.lf.web.service.ResponseInfo;
import com.lf.web.service.ServiceSupport;
import org.apache.log4j.Logger;

public class WebNodeUpdateService extends ServiceSupport {

    private static final Logger LOGGER = Logger.getLogger(WebNodeUpdateService.class);
    private WebNodeDAO webNodeDAO;
    private KeyUpdate keyUpdate = new KeyUpdate(WebNode.class);
    private String type = "";

    public void setWebNodeDAO(WebNodeDAO webNodeDAO) {
        this.webNodeDAO = webNodeDAO;
    }

    public String execute() {
        type = form.getString("type");
        if ("limit".equals(type)) {
            return limit();
        } else {
            return update();
        }
    }

    private String update() {
        boolean result = form.getBean(keyUpdate);
        if (result && keyUpdate.check()) {
            int code = ResponseCode.FINISH;
            try {
                webNodeDAO.update(keyUpdate);
            } catch (Exception e) {
                LOGGER.error("修改异常[" + keyUpdate + "]：" + e);
                code = ResponseCode.FAILURE;
            }
            ResponseInfo msg = new ResponseInfo(code);
            out(msg);
        } else {
            LOGGER.error("异常修改信息：" + keyUpdate);
            error(404);
        }
        return NONE;
    }

    private String limit() {
        Limit limit = new Limit();
        if (limit.load()) {
            context.put("limitConfig", limit.getLimit());
        }
        return "limit";
    }
}
